package net.maku.module.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import net.maku.framework.common.utils.Result;
import net.maku.module.entity.AgHomeConfiguration;
import net.maku.module.service.AgHomeConfigurationService;
import net.maku.module.util.UrlReplaceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/homeConfig")
public class HomeConfigController {

    @Autowired
    private AgHomeConfigurationService service;

    @Autowired
    private UrlReplaceUtil urlReplaceUtil;


    @Operation(summary = "根据id和websiteId查询配置")
    @GetMapping("/get")
    public Result<AgHomeConfiguration> get(@RequestParam Integer id, @RequestParam Integer websiteId) {
        AgHomeConfiguration config = service.getByIdAndWebsite(id, websiteId);
        return config != null ? Result.ok(config) : Result.error("配置不存在");
    }

    @Operation(summary = "根据websiteId查询所有配置")
    @GetMapping("/list")
    public Result<List<AgHomeConfiguration>> list(HttpServletRequest request,@RequestParam Integer websiteId) {
        List<AgHomeConfiguration> configs = service.getByWebsiteId(websiteId);
        urlReplaceUtil.homeConfig(request,configs);
        return Result.ok(configs);
    }

    @Operation(summary = "新增配置")
    @PostMapping("/add")
    public Result<?> add(@RequestBody AgHomeConfiguration config) {
        return service.addConfig(config) > 0 ? Result.ok() : Result.error("新增失败");
    }

    @Operation(summary = "修改配置")
    @PostMapping("/update")
    public Result<?> update(@RequestBody AgHomeConfiguration config) {
        return service.updateConfig(config) > 0 ? Result.ok() : Result.error("修改失败");
    }

    @Operation(summary = "删除配置")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam Integer id, @RequestParam Integer websiteId) {
        return service.deleteConfig(id, websiteId) > 0 ? Result.ok() : Result.error("删除失败");
    }
}